package me.bcloud.web.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import me.bcloud.web.domain.JobConfig;
import me.bcloud.web.domain.User;

@Component
public class UserDaoMysqlJdbcImpl implements UserDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Resource(name = "jobConfigDaoMysqlJdbcImpl")
	private JobConfigDao jobConfigDao;

	@Override
	public List<User> findAll(JobConfig jobConfig) {
		String sql = "select * from " + jobConfig.getReadTable() + " where id>"
				+ jobConfig.getStartid() + " and id<="
				+ (jobConfig.getStartid() + jobConfig.getCacheNum());
		System.out.println(sql);
		final String[] datas = jobConfig.getDatas().split(",");
		// datas先不考虑

		List<User> users = jdbcTemplate.query(sql, new RowMapper<User>() {

			@Override
			public User mapRow(ResultSet set, int index) throws SQLException {

				User user = new User();

				user.setId(set.getInt("id"));
				user.setUserid(set.getString("userid"));
				user.setPassword(set.getString("password"));
				return user;
			}

		});
		jobConfig.setStartid(jobConfig.getStartid() + jobConfig.getCacheNum());

		jobConfigDao.update(jobConfig);
		return users;

	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJobConfigDao(JobConfigDao jobConfigDao) {
		this.jobConfigDao = jobConfigDao;
	}

	public JobConfigDao getJobConfigDao() {
		return jobConfigDao;
	}

	@Override
	public boolean saveUser(String talbe, User user, String s1, String s2) {
		String sql = "insert into " + talbe + "(userid,password," + s2
				+ " value ('" + user.getUserid() + "','" + user.getPassword()
				+ "'," + s1;
		try {
			jdbcTemplate.execute(sql);
		} catch (Exception e) {
			return false;
		}

		return true;
	}

	@Override
	public boolean saveUser(String table, User user) {
		String sql = "insert into " + table + "(userid,password) value('"
				+ user.getUserid() + "','" + user.getPassword() + "')";
		try {
			jdbcTemplate.execute(sql);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public void create(String table) {
		String sql = "CREATE TABLE IF NOT EXISTS "
				+ table
				+ "(id INT AUTO_INCREMENT PRIMARY KEY,userid VARCHAR(50) NOT NULL,password VARCHAR(50) NOT NULL,times TIMESTAMP)ENGINE=InnoDB DEFAULT CHARSET=utf8";
		System.out.println(sql);
		jdbcTemplate.execute(sql);
	}

	public void create(String table, String[] others) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < others.length; i++) {

			if (others[i].equals("int")) {
				sb.append(others[i + 1] + " INT,");
			} else if (others[i].equals("varchar")) {
				sb.append(others[i + 1] + " varchar(255),");
			} else if (others[i].equals("timestamp")) {
				sb.append(others[i + 1] + " TIMESTAMP,");
			}

			i++;

		}

		String sql = "CREATE TABLE IF NOT EXISTS "
				+ table
				+ "(id INT AUTO_INCREMENT PRIMARY KEY,userid VARCHAR(50) NOT NULL,password VARCHAR(50) NOT NULL,times TIMESTAMP,"
				+ sb.substring(0, sb.length() - 1)
				+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8";
		System.out.println(sql);
		jdbcTemplate.execute(sql);

	}

	@Override
	public String getLastTime(String table) {
		String sql = "select times from " + table
				+ " order by times desc limit 1";
		try {
			return jdbcTemplate.query(sql, new ResultSetExtractor<String>() {

				@Override
				public String extractData(ResultSet rs) throws SQLException,
						DataAccessException {
					// TODO Auto-generated method stub

					rs.next();
					return rs.getString(1);

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	@Override
	public int getMaxNum(String table) {
		String sql = "select count(*) from " + table;
		try {
			return jdbcTemplate.query(sql, new ResultSetExtractor<Integer>() {

				@Override
				public Integer extractData(ResultSet rs) throws SQLException,
						DataAccessException {
					// TODO Auto-generated method stub

					rs.next();
					return rs.getInt(1);

				}

			});
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public int getOneDayCount(String table) {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		String sql = "select count(*) from " + table + " where times>'"
				+ sdf.format(date) + "'";
		System.out.println(sql);
		try {
			return jdbcTemplate.query(sql, new ResultSetExtractor<Integer>() {

				@Override
				public Integer extractData(ResultSet rs) throws SQLException,
						DataAccessException {
					// TODO Auto-generated method stub

					rs.next();
					return rs.getInt(1);

				}

			});
		} catch (Exception e) {
			return -1;
		}
	}



}
